package com.ymk.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ymk.base.mybatis.DaoRouter;
import com.ymk.base.mybatis.Pagination;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.user.MchDeskBO;
import com.ymk.core.constants.WxConstants;
import com.ymk.service.IMchDeskService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户表 服务实现类
 * </p>
 *
 * @author 莫参商
 * @since 2023-05-05
 */
@Service
public class MchDeskService implements IMchDeskService {

    private final static Logger logger = LoggerFactory.getLogger(MchDeskService.class);

    @Override
    public MchDeskBO get(Map param) {
        return this.daoRouter.queryForObject("MchDesk.loadMchDesk", param);
    }

    @Override
    public List<MchDeskBO> list(Map param) {
        return this.daoRouter.query("MchDesk.loadMchDesk", param);
    }

    @Override
    public List<MchDeskBO> listForPage(Pagination pagination) {
        return this.daoRouter.query("MchDesk.loadMchDeskForPage", pagination);
    }

    @Override
    public void post(MchDeskBO param) {
        this.daoRouter.insert("AMchDesk.insertMchDesk", param);
    }

    @Override
    public void update(MchDeskBO param) {
        this.daoRouter.update("AMchDesk.updateMchDesk", param);
    }

    @Override
    public void del(MchDeskBO param) {
        this.daoRouter.update("AMchDesk.delMchDesk", param);
    }

    @Override
    public String getUnlimitedQRCode(MchDeskBO mchDeskBO) {

        String scene = "mchId=" + mchDeskBO.getMchId() + "&deskId=" + mchDeskBO.getId();
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("scene", scene)
                .addParam("page", "pages/good/good");

        logger.info("[wx QRCode request ： ]" + paramsBuilder.build());

        String data = HttpUtil.post(WxConstants.CREATE_QRCODE_URL, JSON.toJSONString(paramsBuilder.build()));

        if(StringUtils.isBlank(data)){
            logger.error("生成二维码信息错误! 错误信息：" + data);
        }

        logger.info("[wx QRCode ： ]" + data);
        return data;
    }

    @Autowired
    private DaoRouter daoRouter;

}
